﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Catering.View_Operaction;

namespace Catering.Catering_Page.system
{
    public partial class NoticeInfo_Edit : System.Web.UI.Page
    {
        public int m_NoticeID = 0;
        public string m_States = "添加";
        public string m_EditType = "";
        public string m_ReturnURL = "";

        String FileNameList = string.Empty;
        // 属性注入

        IList<String> fileUploadList = new List<String>();

        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (Request.QueryString["NoticeID"] != null) Int32.TryParse(Request.QueryString["NoticeID"].ToString(), out m_NoticeID);
            if (m_NoticeID > 0 )
            {
                m_States = "修改";
            }
            if (Request.QueryString["EditType"] != null) m_EditType = Request.QueryString["EditType"].ToString();
            if (Request.QueryString["ReturnURL"] != null) m_ReturnURL = HttpUtility.UrlDecode(Request.QueryString["ReturnURL"].ToString());
            #region 上传

            if (m_EditType.Equals("upload"))
            {
                string guid = Guid.NewGuid().ToString();
                try
                {
                    //获取上传的文件数据
                    HttpPostedFile file = Request.Files["Filedata"];
                    string fileName = file.FileName;
                    //由于不同浏览器取出的FileName不同（有的是文件绝对路径，有的是只有文件名），故要进行处理
                    if (fileName.IndexOf('\\') > -1)
                    {
                        fileName = fileName.Substring(fileName.LastIndexOf('\\') + 1);
                    }
                    else if (fileName.IndexOf('/') > -1)
                    {
                        fileName = fileName.Substring(fileName.LastIndexOf('/') + 1);
                    }
                    //上传的目录
                    string uploadDir = "../../UpFile/";
                    string filename = guid + "@@" + fileName;
                    //上传的路径
                    string uploadPath = uploadDir + filename;
                    //保存文件
                    file.SaveAs(Server.MapPath(uploadPath));
                    //FileNameList = Toolss.GetCookie("FILENAMELIST");
                    FileNameList = this.div_hi_FileName.Value;
                    if (!string.IsNullOrEmpty(FileNameList))
                    {
                        //FileNameList = Bind_Data_to_Controls.Decode(FileNameList);
                        FileNameList = FileNameList + "|" + filename;
                    }
                    else
                    {
                        FileNameList = filename;
                    }
                    //Toolss.WriteCookie("FILENAMELIST", Bind_Data_to_Controls.Encode(FileNameList));
                   // HF_FileName.Value = Bind_Data_to_Controls.Encode(FileNameList);
                    //下面这句代码缺少的话，上传成功后上传队列的显示不会自动消失
                   
                    this.btnEdit.Visible = true;
                    //Response.Write("1");
                  // Response.Write(("<script>alert('" + Bind_Data_to_Controls.Encode(FileNameList) + "！');</script>"));
                    //Response.Write(Bind_Data_to_Controls.Encode(FileNameList) );
                    Response.Write(FileNameList);
                    Response.End();

                }
                catch
                {
                    
                }
            }

            #endregion
            //if (!IsPostBack)
            //{


            #region 初始化数据

            if (this.btnEdit.Visible==false)
            {
                //this.ClearContent(); //清空数据
                if (m_NoticeID > 0)
                {
                    Catering_Model.Announcement model = new Catering_Rules.Announcement().GetModel(m_NoticeID);
                    this.SetContent(model);
                    this.btnAdd.Visible = false;
                    this.btnEdit.Visible = true;
                }
            }

            #endregion
            //}
        }

        /// <summary>
        /// 清空数据
        /// </summary>
        public void ClearContent()
        {
            txt_Title.Text = "";                                        //标题
            ftxt_Conent.Value = "aa";                                      //内容
            div_hi_time.Value = DateTime.Now.ToString("yyyy-MM-dd hh:mm");    //时间
            div_hi_userid.Value = "0";                                  //编辑人
            div_hi_id.Value = "0";                                      //标识列
            allFileName.InnerText = "";                                 //附件名称
        }

        /// <summary>
        /// 设置数据
        /// </summary>
        /// <param name="model"></param>
        public void SetContent(Catering_Model.Announcement model)
        {
            if (model != null)
            {
                txt_Title.Text = model.Title;
                ftxt_Conent.Value = model.Conent;
                div_hi_time.Value = model.Release.ToString("yyyy-MM-dd hh:mm");
                div_hi_userid.Value = model.UserId.ToString();
                div_hi_id.Value = model.id.ToString();
                string fileList = model.temp1;
                string nameList = string.Empty;
                if (!string.IsNullOrEmpty(fileList))
                {
                    this.div_hi_FileName.Value = fileList;
                   // this.div_hi_FileName.Value = Bind_Data_to_Controls.Encode(fileList);
                   // Toolss.WriteCookie("FILENAMELIST", Bind_Data_to_Controls.Encode(fileList));
                    fileUploadList = fileList.Split('|');
                }
                foreach (var item in fileUploadList)
                {

                    if (!string.IsNullOrEmpty(item))
                    {
                        IList<String> list = new List<String>();
                        list = item.Split(new string[1] { "@@" }, StringSplitOptions.None);
                        if (list.Count > 1)
                        {
                            if (!string.IsNullOrEmpty(nameList)) nameList = nameList + "、" + list[1];
                            else nameList = list[1];
                        }
                    }
                    allFileName.InnerText = nameList;
                }
            }
        }

        protected void btnInfoAdd_Click(object sender, EventArgs e)
        {
            Catering_Model.Announcement model = new Catering_Model.Announcement();
            model.Title = txt_Title.Text;
            model.Conent = ftxt_Conent.Value;
            model.Release = DateTime.Now;
            string fileList = this.div_hi_FileName.Value;
            //string fileList = Toolss.GetCookie("FILENAMELIST");
            if (!string.IsNullOrEmpty(fileList))
            {
                //fileList = Bind_Data_to_Controls.Decode(fileList);
                model.temp1 = fileList;
            }
            else
            {
                model.temp1 = "";
            }
            model.temp2 = "";
            model.UserId = Convert.ToInt32(Session["UserID"]);
            new Catering_Rules.Announcement().Add(model);
            Response.Redirect(m_ReturnURL);
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            Catering_Model.Announcement model = new Catering_Model.Announcement();
            model.Title = txt_Title.Text;
            model.Conent = ftxt_Conent.Value;
            model.Release = DateTime.Parse(div_hi_time.Value.Length < 6 ? DateTime.Now.ToShortDateString() : div_hi_time.Value);
            model.UserId = Convert.ToInt32(Session["UserID"].ToString());
            model.id = Convert.ToInt32(div_hi_id.Value);
           // string fileList = Toolss.GetCookie("FILENAMELIST");
            string fileList = this.div_hi_FileName.Value;
            if (!string.IsNullOrEmpty(fileList))
            {
                //fileList = Bind_Data_to_Controls.Decode(fileList);
                model.temp1 = fileList;
            }
            if (new Catering_Rules.Announcement().Update(model))
            {
                Response.Redirect(m_ReturnURL);
            }
        }
    }
}